﻿using Common.Contract;
using Common.Contract.Authentication.Record;
using Common.Contract.ProductHandbook.Request;
using Common.Service.Command;
using CrossSelling.Model.Interface;
using CrossSelling.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CrossSelling.Core.ServiceCommand.Admin
{
    public class AddProductInfoServiceCommand : ProviderServiceCommand<IProviderFactory, AddProductInfoRequest, ResponseRecord>
    {
        private UserRecord Dao;
        private int ProductID;
        public AddProductInfoServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(AddProductInfoRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.Record == null)
            {
                throw new ArgumentException("Record must be set", "request");
            }
            else
            {
                ProductID = request.Record.ProductId;
            }
            if (request.CurrentDao == null)
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.CurrentDao;
            }
        }

        protected override bool OnExecute(ResponseRecord response)
        {
            try
            {
                bool isUpdate = true;
                ProductHandbookProduct checkProductInfo = Providers.ProductHandbooks.RetrieveProductByProductId(Request.Record.ProductId);
                Dictionary<int, ProductHandbookProductDetail> listProductDetail = Providers.ProductHandbooks.RetrieveListProductDetail().ToDictionary(x => x.ProductId);
                if (checkProductInfo != null && !listProductDetail.ContainsKey(checkProductInfo.ProductId))
                {
                    ProductHandbookProductDetail productDetail = new ProductHandbookProductDetail
                    {
                        ProductId = ProductID,
                        ProductName = checkProductInfo.ProductName,
                        ProductNameEn = checkProductInfo.ProductNameEn,
                        ConditionProduct = string.Empty,
                        ConditionProductEn = string.Empty,
                        DocumentProduct = string.Empty,
                        DocumentProductEn = string.Empty,
                        XSellingProduct = string.Empty,
                        XSellingProductEn = string.Empty,
                        OverviewProduct = string.Empty,
                        OverviewProductEn = string.Empty
                    };
                    isUpdate = Providers.ProductHandbooks.AddProductDetail(productDetail);
                }
                else
                {
                    isUpdate = false;
                    response.ErrorMessage = "Product not exists";
                }
                return isUpdate;
            }
            catch (Exception e)
            {
                throw new ArgumentException(e.Message);
            }
        }
    }
}
